package com.coocaa.ops.admin.v1.buttonmanage.validator;


import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.v1.buttonmanage.model.ButtonManageModel;
import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;

/**
 * @author bijiahao
 * @date : 2018/6/14.
 * @description
 */
@Component
public class ButtonManageValidator {
    private static CommonLanguageProperties commonLanguageProperties;

    public static void valid(ButtonManageModel model, HttpServletRequest request, boolean validVersion) {
        //校验登录是否已过时
        WebUtil.getUserInfo(request);
        Assert.notNull(model.getStatus(), commonLanguageProperties.getResourceStatus() + commonLanguageProperties.getNotNull());
        Assert.notNull(model.getResourceId(), commonLanguageProperties.getResourceId() + commonLanguageProperties.getNotNull());
        if (validVersion && !isNotVersioned(model.getType())) {
            Assert.notNull(model.getVersion(), commonLanguageProperties.getResourceVersion() + commonLanguageProperties.getNotNull());
        }
        Assert.notNull(model.getType(), commonLanguageProperties.getResourceType() + commonLanguageProperties.getNotNull());
        Assert.notNull(model.getMenuElId(), "menuElId" + commonLanguageProperties.getNotNull());
    }

    @Autowired
    public void setEnterKeyLetterArt(CommonLanguageProperties commonLanguageProperties) {
        ButtonManageValidator.commonLanguageProperties = commonLanguageProperties;
    }

    /**
     * 没有版本的资源
     *
     * @param type
     * @return
     */
    public static boolean isNotVersioned(String type) {
        return ResourceTypeEnum.POSTER.getValue().equals(type)
                || ResourceTypeEnum.LAYOUT.getValue().equals(type)
                || ResourceTypeEnum.CORNER_ICON.getValue().equals(type)
                ;
    }
}
